export default class JingDongUtils {
    private static instance: JingDongUtils;

    public static getInstance() {
        if (!this.instance) {
            this.instance = new JingDongUtils();
        }
        return this.instance;
    }

    private mUzkJDopen: ApicloudModules['uzkJDopen'];

    private constructor() {
        this.mUzkJDopen = api.require('uzkJDopen')
        if (!this.mUzkJDopen) {
            throw new Error('uzkJDopen 模块没引入')
        }
    }
    /**
    * 初始化,全局调用一次
    */
    public init() {
        return new Promise<{ status: boolean, msg: string }>(res => {
            this.mUzkJDopen.init((ret, err) => {
                if (ret && ret.status) {
                    res({
                        status: true,
                        msg: '初始成功'
                    });
                } else {
                    res({
                        ...err,
                        status: false,
                    });
                }
            })
        });
    }

    /**
     * 打开链接
     * @param params 
     */
    public openByUrl(params: { url: string }) {
        return new Promise<{ status: boolean, msg: string }>(res => {
            this.mUzkJDopen.openurl(params, (ret, err) => {
                if (ret) {
                    res(ret);
                } else {
                    res(err);
                }
            });
        });
    }

    /**
     * 判断是否安装
     */
    public installed() {
        const bundleId = api.systemType == 'ios' ? 'openapp.jdmobile://' : 'com.jingdong.app.mall';
        const installed = api.appInstalled({
            sync: true,
            appBundle: bundleId
        });
        return installed;
    }

}
